import type { Key } from 'react';

type ScrollToOpts = {
  end: number;
  start: number;
  reduce?: boolean;
}

type ScrollToLeftCallback = (
  val: number
) => void;

export const scrollTo = (callback: ScrollToLeftCallback, opts: ScrollToOpts) => {
  let step: Key = 8;
  let { start } = opts;

  if (opts.reduce) {
    step = '-' + step
  };

  const timer = setInterval(() => {
    start += (+step);
    if (start >= opts.end) {
      start = opts.end;
      clearInterval(timer);
    }
    callback(start);
  }, 20);
  return timer;
}

export const getPosition = (evt: MouseEvent, el: HTMLDivElement, offset = 0) => {
  return {
    x: evt.clientX - el.offsetLeft - offset,
    y: evt.clientY - el.offsetTop - offset
  }
}